package com.macro.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.UmsCommunityMapper;
import com.macro.mall.model.UmsCommunity;
import com.macro.mall.service.UmsCommunityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * UmsCommunity的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class UmsCommunityServiceImpl implements UmsCommunityService {

    @Value("${log.isDebugEnabled:false}")
    private boolean isDebugEnabled;

    @Autowired
    private UmsCommunityMapper umsCommunityMapper;

    @Override
    public List<UmsCommunity> find(String province, String cityCode, String countyCode, String communityName, Integer pageSize, Integer pageNum) {
        //TODO这里可以做通过Assist做添加查询
        PageHelper.startPage(pageNum, pageSize);
        Assist assist = new Assist();
        if (StrUtil.isNotEmpty(province)) {
            assist.andLike("province", "%" + province + "%");
        }
        if (StrUtil.isNotEmpty(cityCode)) {
            assist.andLike("city_Code", "%" + cityCode + "%");
        }
        if (StrUtil.isNotEmpty(countyCode)) {
            assist.andLike("county_Code", "%" + countyCode + "%");
        }
        if (StrUtil.isNotEmpty(communityName)) {
            assist.andLike("community_name", "%" + communityName + "%");
        }
        assist.setOrder(Assist.order("create_time", false));
        List<UmsCommunity> result = umsCommunityMapper.selectUmsCommunity(assist);
        if (isDebugEnabled) {
            log.debug("执行获取UmsCommunity数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public UmsCommunity findOne(Long id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过UmsCommunity的id获得UmsCommunity对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过UmsCommunity的id获得UmsCommunity对象-->失败:id不能为空");
        }
        UmsCommunity result = umsCommunityMapper.selectUmsCommunityById(id);
        if (isDebugEnabled) {
            log.debug("执行通过UmsCommunity的id获得UmsCommunity对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(UmsCommunity value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将UmsCommunity中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        if (!StrUtil.isBlankIfStr(value.getEndTime()) && !StrUtil.isBlankIfStr(value.getStartTime())) {
            if (value.getEndTime().getTime() < value.getStartTime().getTime()) {
                Asserts.fail("合作开始时间应早于结束时间");
            }
            long l = System.currentTimeMillis();
            if (value.getStartTime().getTime() < l && value.getEndTime().getTime() > l) {
                value.setStatus(1);
            } else {
                value.setStatus(0);
            }
        }
        int result = umsCommunityMapper.insertNotNullUmsCommunity(value);
        if (isDebugEnabled) {
            log.debug("执行将UmsCommunity中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(UmsCommunity value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过UmsCommunity的id更新UmsCommunity中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过UmsCommunity的id更新UmsCommunity中属性不为null的数据-->失败:对象为null");
        }
        if (!StrUtil.isBlankIfStr(value.getEndTime()) && !StrUtil.isBlankIfStr(value.getStartTime())) {
            if (value.getEndTime().getTime() < value.getStartTime().getTime()) {
                Asserts.fail("合作开始时间应早于结束时间");
            }
            if(null == value.getStatus()) {
                long l = System.currentTimeMillis();
                if (value.getStartTime().getTime() < l && value.getEndTime().getTime() > l) {
                    value.setStatus(1);
                } else {
                    value.setStatus(0);
                }
            }
        }
        int result = umsCommunityMapper.updateNotNullUmsCommunityById(value);
        if (isDebugEnabled) {
            log.debug("执行通过UmsCommunity的id更新UmsCommunity中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Long id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过UmsCommunity的id删除UmsCommunity-->失败:id不能为空");
            }
            Asserts.fail("执行通过UmsCommunity的id删除UmsCommunity-->失败:id不能为空");
        }
        int result = umsCommunityMapper.deleteUmsCommunityById(id);
        if (isDebugEnabled) {
            log.debug("执行通过UmsCommunity的id删除UmsCommunity-->结果:", result);
        }
        return CommonResult.success(null);
    }


}